﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace BanHang
{
    public partial class HoaDon : Form
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
        SqlDataAdapter dap;
        DataSet ds;
        SqlCommand cmd;
        double TongTien;

        public HoaDon()
        {
            InitializeComponent();
        }

        private void loadCBKH()
        {
            dap = new SqlDataAdapter("select * from khachhang",conn);
            ds = new DataSet();
            dap.Fill(ds, "khachhang");

            comboBox1.DataSource = ds.Tables["khachhang"];
            comboBox1.DisplayMember = "MaKhachHang";
            comboBox1.ValueMember = "MaKhachHang";

            txtTenKH.DataBindings.Add("Text", ds.Tables["khachhang"],"TenKhachHang");
            txtDiaChi.DataBindings.Add("Text", ds.Tables["khachhang"], "DiaChi");
            txtDienThoai.DataBindings.Add("Text", ds.Tables["khachhang"], "DienThoai");

        }

        private void loadCBSP()
        {

            dap = new SqlDataAdapter("select * from sanpham", conn);
            ds = new DataSet();
            dap.Fill(ds, "sanpham");

            comboBox2.DataSource = ds.Tables["sanpham"];
            comboBox2.DisplayMember = "MaSanPham";
            comboBox2.ValueMember = "MaSanPham";

            txtSanPham.DataBindings.Add("Text", ds.Tables["sanpham"], "TenSanPHam");
            txtGia.DataBindings.Add("Text", ds.Tables["sanpham"], "Gia");
             
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            loadCBKH();
            loadCBSP();
        }

        private void Tong_Tien()
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1;i++ )
            {
                TongTien += (double.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString()) * double.Parse(dataGridView1.Rows[i].Cells[4].Value.ToString()));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double thanhtien = 0.0;
            
            thanhtien = double.Parse(txtSoLuong.Text) * double.Parse(txtGia.Text);
            dataGridView1.Rows.Add(dataGridView1.Rows.Count.ToString(), txtMaHD.Text, comboBox2.Text, txtGia.Text, txtSoLuong.Text,thanhtien.ToString());
            Tong_Tien();
            textBox8.Text = TongTien.ToString();
            TongTien = 0.0;
        }

        private void button2_Click(object sender, EventArgs e)
        {

            int i = LuuHD(txtMaHD.Text, Convert.ToString(dateTimePicker1.Value), int.Parse(comboBox1.Text), double.Parse(textBox8.Text));
            if (i == 1)
            {
                for (int j = 0; j < dataGridView1.Rows.Count - 1; j++)
                {
                    LuuHDCT(txtMaHD.Text, int.Parse(comboBox2.Text), double.Parse(dataGridView1.Rows[j].Cells[3].Value.ToString()), int.Parse(dataGridView1.Rows[j].Cells[4].Value.ToString()));
                }
            }

             
        }

        private int LuuHD(string Ma, string ngay, int maKH, double tongtien)
        {
            conn.Open();
            cmd = new SqlCommand("Insert into HoaDon values('" + Ma + "','" + ngay + "'," + maKH + "," + tongtien + ")", conn);
            int i = cmd.ExecuteNonQuery();
            conn.Close();
            return i;
        }

        private int LuuHDCT(string MaHD, int HSP, double Gia, int SoLuong)
        {
            conn.Open();
            cmd = new SqlCommand("insert into ChiTietHoaDon values('"+MaHD+"',"+HSP+","+Gia+","+SoLuong+")", conn);
            int i = cmd.ExecuteNonQuery();

            conn.Close();
            return i;
        }
    }
}
